package com.ruihang.generalibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class FontSizeScaleUtil {

    public final static int FONT_SMALL = 0;
    public final static int FONT_MID = 1;
    public final static int FONT_BIG = 2;

    public static void scaleFontSize(Activity act, int curType, int targetType) {

        int size = (targetType - curType) * 8;

        List<View> views = getAllChildViews(act);
        for (View view : views) {
            if (view instanceof TextView) {
                try {
                    TextView tv = (TextView) view;
//                    String txt = String.valueOf(tv.getText());
                    long id = tv.getId();
//                    if (id == R.id.fixed_bottom_navigation_title || id == R.id.shifting_bottom_navigation_title || id == R.id.menu_item_sec_txt || id == R.id.menu_fir_txt) {
//                        size = (targetType - curType) * 2;
//                    }
                    float currSize = tv.getTextSize();
                    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, currSize + size);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        // 缓存字体大小
        SharedPreferencesUtil.setParam(act, "SETTING_FONT_SIZE", targetType);
    }

    public static void scalFontSizeByView(View view, Context context) {

        int fontType = (int) SharedPreferencesUtil.getParam(context, "SETTING_FONT_SIZE", 1);
//                    FontSizeScaleUtil.scaleFontSize(this, 1, fontType);
        int size = (fontType - 1) * 10;
//        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, currSize + size);
//        int size = (targetType - curType) * 10;
        List<View> views = getAllChildViews(view);
        for (View view1 : views) {
            if (view1 instanceof TextView) {
                try {
                    TextView tv = (TextView) view1;
//                    String txt = String.valueOf(tv.getText());
                    float currSize = tv.getTextSize();
                    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, currSize + size);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static List<View> getAllChildViews(Activity act) {
        View view = act.getWindow().getDecorView();
        return getAllChildViews(view);
    }

    private static List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup && !(view instanceof ListView)) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
}
